package com.ruoyi.applet.product.service.impl;

import com.ruoyi.applet.entity.PageR;
import com.ruoyi.applet.entity.ProductApplet;
import com.ruoyi.applet.product.domain.Product;
import com.ruoyi.applet.product.domain.ProductCategories;
import com.ruoyi.applet.product.mapper.ProductMapper;
import com.ruoyi.applet.product.service.IProductService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品Service业务层处理
 * @author ruoyi
 * @date 2024-11-20
 */
@Service
public class ProductServiceImpl implements IProductService {
    
    @Resource
    private ProductMapper productMapper;
    
    /**
     * 查询产品
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public Product selectProductById(Long id) {
        return productMapper.selectProductById(id);
    }
    
    /**
     * 查询产品列表
     * @param product 产品
     * @return 产品
     */
    @Override
    public List<Product> selectProductList(Product product) {
        return productMapper.selectProductList(product);
    }
    
    /**
     * 新增产品
     * @param product 产品
     * @return 结果
     */
    @Override
    public int insertProduct(Product product) {
        product.setCreateTime(DateUtils.getNowDate());
        return productMapper.insertProduct(product);
    }
    
    /**
     * 修改产品
     * @param product 产品
     * @return 结果
     */
    @Override
    public int updateProduct(Product product) {
        product.setUpdateTime(DateUtils.getNowDate());
        return productMapper.updateProduct(product);
    }
    
    /**
     * 批量删除产品
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(String ids) {
        return productMapper.deleteProductByIds(Convert.toStrArray(ids));
    }
    
    /**
     * 删除产品信息
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id) {
        return productMapper.deleteProductById(id);
    }
    
    /**
     * 小程序 - 查询产品列表
     * @param product 条件
     * @return {@link List<ProductCategories>}
     */
    @Override
    public PageR<Product> selectProductListApplet(ProductApplet product) {
        int count = productMapper.selectProductListAppletCount(product);
        PageR<Product> pageR = new PageR<>();
        pageR.setPageNo(product.getPageNo());
        pageR.setPageSize(product.getPageSize());
        pageR.setTotal(count);
        if (count == 0) {
            return pageR;
        }
        int start = (product.getPageNo() - 1) * product.getPageSize();
        List<Product> productAppletList = productMapper.selectProductListApplet(product, product.getPageSize(), start);
        
        pageR.setReCords(productAppletList);
        return pageR;
    }
    
    /**
     * 小程序 - 获取热门产品
     * @param product 条件
     * @return {@link PageR<Product>}
     */
    @Override
    public PageR<Product> selectProductListByHotApplet(ProductApplet product) {
        int count = productMapper.selectProductListHotAppletCount(product);
        PageR<Product> pageR = new PageR<>();
        pageR.setPageNo(product.getPageNo());
        pageR.setPageSize(product.getPageSize());
        pageR.setTotal(count);
        if (count == 0) {
            return pageR;
        }
        int start = (product.getPageNo() - 1) * product.getPageSize();
        List<Product> productAppletList = productMapper.selectProductListByHotApplet(product, product.getPageSize(), start);
        
        pageR.setReCords(productAppletList);
        return pageR;
    }
    
    /**
     * 小程序 - 获取最新产品
     * @param product 条件
     * @return {@link PageR<Product>}
     */
    @Override
    public PageR<Product> selectProductListByNewApplet(ProductApplet product) {
        int count = productMapper.selectProductListNewAppletCount(product);
        PageR<Product> pageR = new PageR<>();
        pageR.setPageNo(product.getPageNo());
        pageR.setPageSize(product.getPageSize());
        pageR.setTotal(count);
        if (count == 0) {
            return pageR;
        }
        int start = (product.getPageNo() - 1) * product.getPageSize();
        List<Product> productAppletList = productMapper.selectProductListByNewApplet(product, product.getPageSize(), start);
        
        pageR.setReCords(productAppletList);
        return pageR;
    }
}
